﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class PCBA_PCBAUnion_PCBAUnionAdd : System.Web.UI.Page
{
    private PCBAUnion pu;
    private DataRow dr;
    private DataTable pamdt
    {
        get
        {
            return (DataTable)ViewState["pamdt"];
        }
        set
        {
            ViewState["pamdt"] = value;
        }
    }

    public PCBA_PCBAUnion_PCBAUnionAdd()
    {
        pu = new PCBAUnion();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindJointingFailure();
            pamdt = new DataTable();
            pamdt.Columns.Add(new DataColumn("PCBASN", Type.GetType("System.String")));
            pamdt.Columns.Add(new DataColumn("UserID", Type.GetType("System.String")));
            pamdt.Columns.Add(new DataColumn("FailureName", Type.GetType("System.String")));
        }
    }

    protected void BindJointingFailure()
    {
        JointingFailure jf = new JointingFailure();
        this.DropDownList1.DataSource = jf.JointingFailure_GetbyMan();
        this.DropDownList1.DataValueField = "FailureName";
        this.DropDownList1.DataTextField = "FailureName";
        this.DropDownList1.DataBind();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup2");
        if (Page.IsValid)
        {
            dr = pamdt.NewRow();
            dr["PCBASN"] = this.TextBox2.Text.Trim();
            dr["UserID"] = this.TextBox3.Text.Trim();
            dr["FailureName"] = this.DropDownList1.SelectedValue;
            if (pamdt.Select("PCBASN = '" + this.TextBox2.Text.Trim() + "'").Length == 0)
            {
                pamdt.Rows.InsertAt(dr, 0);
                Bindpam();
                this.TextBox2.Text = "";
                this.TextBox2.Focus();
            }
            else
            {
                string str = "已经在列表中";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        this.TextBox2.Focus();
    }

    protected void Bindpam()
    {
        this.GridView1.DataSource = pamdt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        pamdt.Select("PCBASN = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "'")[0].Delete();
        Bindpam();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup1");
        if (Page.IsValid)
        {
            string str = "";
            if (pamdt.Rows.Count < 1)
            {
                str = "请先添加要关联PCBA板信息!";
            }
            else
            {
                string productSN = this.TextBox1.Text.Trim();
                if (CommHelper.IsValidSN_forDataAcquisition(productSN))
                {
                    productSN = CommHelper.GetProductSN(productSN);
                    DateTime dt = DateTime.Now;
                    PCBAUnionInfo padi = new PCBAUnionInfo(productSN, dt, User.Identity.Name);
                    if (pu.PCBAUnionItem_Insert(padi, pamdt))
                    {
                        this.TextBox1.Text = "";
                        pamdt.Clear();
                        Bindpam();
                        this.TextBox2.Focus();
                        str = productSN + "提交成功!";
                    }
                    else
                    {
                        str = "提交失败，请联系相关人员!";
                    }
                }
                else
                {
                    str = "序列号不符合腔体序列号编码规则，或不是关联过的成品序列号，请检查!";
                }
            }
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }
}
